/**
 * Created by levy on 16/7/21.
 */
import {shopping} from "../../const/shopping";

/**
 * 传 data 渲染
 * 有需要时传 salePrice, 因为不同情况下, 售价计算方式不同
 * 有需要时传 count, 比较立即购买, 数量只能为1
 *
 * doAction true: 此时点击去到商品详情
 */
const Item = React.createClass({
    componentDidMount() {
        React.findDOMNode(this.refs.item).addEventListener('touchend', () => {
            if(this.props.doAction) {
                liefeng.appJs.doAction(`shopping-item-detail.html?goodsId=${this.props.data.goodsId}`,
                    this.props.data.goodsName);
            }
        });
    },
    render() {
        let item = this.props.data,
            salePrice = this.props.salePrice || 
                item.promotePrice || item.shopPrice || item.goodsPrice || '0',
            count = this.props.count || item.goodsNumber || '0';

        return <div className="item flex border-bottom" ref="item">
            <img src={item.goodsThumb ? liefeng.utils.getProtocol() + shopping.imgPre + item.goodsThumb : ''} className="img"/>
            <div className="text flex">
                <div className="info flex col align-start">
                    <p className="name">{item.goodsName}</p>
                    <div className="item-count">
                        {'数量: ' + count}
                    </div>
                </div>
                <div className="price flex col">
                    <span className="sale-price num">{"￥" + salePrice}</span>
                    <span className="origin-price num">{"￥" + (item.marketPrice || '0')}</span>
                </div>
            </div>
        </div>
    }
});

export default Item;
